package com.zmn.mcc.services.impl.module;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.module.McModule;
import com.zmn.mcc.model.entity.module.McModuleQuery;
import com.zmn.mcc.persistence.interfaces.module.ModuleDao;
import com.zmn.mcc.services.interfaces.module.ModuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 14:42
 */
@Service
public class ModuleServiceImpl implements ModuleService {

    @Resource
    private ModuleDao moduleDao;

    @Override
    public McModule findByKey(Integer moduleId) {
        return moduleDao.findByKey(moduleId);
    }

    @Override
    public List<McModule> listPageByQuery(McModuleQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : moduleDao.listPageByQuery(query);
    }

    @Override
    public List<McModule> listAll() {
        return this.listAll(null);
    }

    @Override
    public List<McModule> listAll(Integer status) {
        McModuleQuery query = new McModuleQuery();
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McModule> listBySysId(Integer sysId) {
        return this.listBySysId(sysId, null);
    }

    @Override
    public List<McModule> listBySysId(Integer sysId, Integer status) {
        McModuleQuery query = new McModuleQuery();
        query.setSysId(sysId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McModule> listByParentId(Integer sysId, Integer parentId) {
        return this.listByParentId(sysId, parentId, null);
    }

    @Override
    public List<McModule> listByParentId(Integer sysId, Integer parentId, Integer status) {
        McModuleQuery query = new McModuleQuery();
        query.setSysId(sysId);
        query.setParentId(parentId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McModule> listByQuery(McModuleQuery query) {
        return moduleDao.listByQuery(query);
    }

    @Override
    public Integer countByQuery(McModuleQuery query) {
        return moduleDao.countByQuery(query);
    }


    @Override
    public Integer updateByKey(McModule module) {
        if (module.getUpdateTime() == null) {
            module.setUpdateTime(DateUtil.getNow());
        }
        return moduleDao.updateByKey(module);
    }

    @Override
    public Integer updateStatus(Integer moduleId, Integer status, String updater) {
        McModule module = new McModule();
        module.setModuleId(moduleId);
        module.setStatus(status);
        module.setUpdater(updater);
        return this.updateStatus(module);
    }

    @Override
    public Integer updateStatus(McModule module) {
        if (module.getUpdateTime() == null) {
            module.setUpdateTime(DateUtil.getNow());
        }
        return moduleDao.updateByKey(module);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> moduleIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(moduleIds);
        dto.setStatus(status.intValue());
        dto.setUpdater(updater);
        return updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return moduleDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateSort(Integer moduleId, Integer sort) {
        McModule module = new McModule();
        module.setModuleId(moduleId);
        module.setSort(sort);
        return this.updateSort(module);
    }

    @Override
    public Integer updateSort(McModule module) {
        return moduleDao.updateSort(module);
    }


    @Override
    public Integer insert(McModule module) {
        if (NumberUtil.isNullOrZero(module.getParentId())) {
            module.setParentId(GlobalConsts.TOP_ID);
        }
        module.setSort(100);
        module.setStatus(GlobalDict.STATUS_ENABLE);
        if (module.getUpdateTime() == null) {
            module.setUpdateTime(DateUtil.getNow());
        }
        module.setCreater(module.getUpdater());
        module.setCreateTime(module.getUpdateTime());
        return moduleDao.insert(module);
    }


    @Override
    public Integer deleteByKey(Integer moduleId) {
        return moduleDao.deleteByKey(moduleId);
    }

    @Override
    public Integer deleteBySysId(Integer sysId) {
        McModuleQuery query = new McModuleQuery();
        query.setSysId(sysId);
        return this.deleteByQuery(query);
    }

    @Override
    public Integer deleteByParentId(Integer parentId) {
        McModuleQuery query = new McModuleQuery();
        query.setParentId(parentId);
        return this.deleteByQuery(query);
    }

    @Override
    public Integer deleteByQuery(McModuleQuery query) {
        return moduleDao.deleteByQuery(query);
    }
}
